package inherit.define.polymorphism.practice02;

public class Person {
    String name;
    int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // 多态的规则，一个就是调用规则，一个就是强制转换的一个规则，如何判断instanceof判断不同的类型，
    /*public void keepPet(Dog d,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+d.getColor()+"颜色"+d.getAge()+"岁的狗");
        d.eat(something);
    }

    public void keepPet(Cat c,String something){
        System.out.println("年龄为"+age+"岁的"+name+"养了一只"+c.getColor()+"颜色"+c.getAge()+"岁的猫");
        c.eat(something);
    }*/
    // 这两个代码可以使用多态进行实践
    public void keepPet(Animal a,String something){
        if (a instanceof Dog d){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+d.getColor()+"颜色"+d.getAge()+"岁的狗");
            d.eat(something);
            d.lookHome();
        }
        else if (a instanceof Cat c){
            System.out.println("年龄为"+age+"岁的"+name+"养了一只"+c.getColor()+"颜色"+c.getAge()+"岁的猫");
            c.eat(something);
            c.catMouse();
        }
    }
}
